<?php
namespace app\admin\controller;

use think\Controller;

class Authrule extends Base
{
    //查询
    public function index(){

        $res=db('auth_rule')->select();
        return view('',['res'=>$res]);
    }

    //添加二级菜单
    public function add(){

        $res=db('auth_rule')->where('condition',1)->select();
        if(request()->isAjax())
        {
            $data=input('post.');
            $title=$data['title'];
            $name=$data['name'];
            $pid=$data['pid'];
            $res=db('auth_rule')
                ->insert([
                    'title'=>$title,
                    'name'=>$name,
                    'pid'=>$pid,
                    'condition'=>2,
                ]);
            if($res){
                return array('code'=>1,'msg'=>"添加成功");
            }else{
                return array('code'=>0,'msg'=>"添加失败");
            }

        }
        else
        {
            return view('',['auth'=>$res]);
        }
    }

    //修改二级菜单
    public function updatetwo(){

        $id=input('id');

        $res=db('auth_rule')->where('id',$id)->find();
        //查当前id的上级菜单
        $resMenu=db('auth_rule')->where('id',$res['pid'])->find();
        //halt($resMenu);
        //查所有上级菜单
        $allMenu=db('auth_rule')->where('condition',1)->select();
        //halt($res);
        if(request()->isAjax()){
            //$data=input('post.');
            //halt($data);
            $id=input('id');
            $res=db('auth_rule')
                ->where('id',$id)
                ->update([
                    'title'=>input('title'),
                    'name' =>input('url'),
                    'pid'  =>input('pid'),
                ]);
            if($res){
                return array('code'=>1,'msg'=>"修改成功");
            }else{
                return array('code'=>0,'msg'=>"修改失败");
            }
        }
        else{
            return view('',['id'=>$id,'res'=>$res,'rs'=>$allMenu,'resMenu'=>$resMenu]);
        }
    }

}
